﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;


namespace LY.PdfToWord
{
    public class ToPdf : Controller
    {
        private LabelName _label = new LabelName();
        public ToPdf(LabelName label)
        {
            _label = label;
        }

        public void Toprint()
        {
            WriteIntoWord wiw = new WriteIntoWord();
            string SaveDocPath = "", filename = "";
            try
            {
                //string FilePath = Server.MapPath("005借款协议.dot"); //模板路径 
                string FilePath = System.AppDomain.CurrentDomain.BaseDirectory + "Investor_protocol.dot";//投资人协议

                filename = _label.Number;

                SaveDocPath = System.AppDomain.CurrentDomain.BaseDirectory + filename + ".doc";
                wiw.OpenDocument(FilePath);
            }
            catch (Exception)
            {
                throw;
            }
            //写入书签
            wiw.WriteIntoDocument("Number", _label.Uniqueness);//全局金服ID号
            wiw.WriteIntoDocument("RealName", _label.RealName); // 乙方  （前）
            wiw.WriteIntoDocument("RealName1", _label.RealName1); // 乙方  （后）
            wiw.WriteIntoDocument("IDCode", _label.IDCode);//乙方身份证号
            wiw.WriteIntoDocument("ConcludeDate", _label.ConcludeDate);//协议签订日期


            /* wiw.WriteIntoDocument("IDcard", _label.IDcard);//乙方身份证号下

             wiw.WriteIntoDocument("RmBlowercase", _label.RmBlowercase); // 人民币小写
             wiw.WriteIntoDocument("RmBcapital", _label.RmBcapital); //人民币大写
             wiw.WriteIntoDocument("BorrowRate", _label.BorrowRate);//借款年化利率

             wiw.WriteIntoDocument("BorrowLimit", _label.BorrowLimit); //借款期限
             wiw.WriteIntoDocument("ValueDate", _label.ValueDate); //起息日
             wiw.WriteIntoDocument("RefundDay", _label.RefundDay);//还款日

             wiw.WriteIntoDocument("RefundWay", _label.RefundWay);//还款方式(等额本息、按月还款/到期一次性还本付息)
             wiw.WriteIntoDocument("HousePlace", _label.HousePlace);//房屋位置（担保方式）
             wiw.WriteIntoDocument("RefundPrincipal", _label.RefundPrincipal);  //月偿还本息金额*/




            wiw.Save_CloseDocument(SaveDocPath);

            //转成pdf 
            string SavePdfPath = System.AppDomain.CurrentDomain.BaseDirectory + "contract/" + filename + ".pdf";
            var flag = wiw.WordToPdf(SaveDocPath, SavePdfPath);
            if (flag)
            {
                //删除临时word, 
                System.IO.File.Delete(SaveDocPath);
            }
        }


        public void Toprint1()
        {
            WriteIntoWord wiw = new WriteIntoWord();
            string SaveDocPath = "", filename = "";
            try
            {
                //string FilePath = Server.MapPath("005借款协议.dot"); //模板路径 
                string FilePath = System.AppDomain.CurrentDomain.BaseDirectory + "Borrower_protocol.dot";//借款人协议

                filename = _label.Number + "_Borrower";

                SaveDocPath = System.AppDomain.CurrentDomain.BaseDirectory + filename + ".doc";
                wiw.OpenDocument(FilePath);
            }
            catch (Exception)
            {
                throw;
            }
            //写入书签
            wiw.WriteIntoDocument("Number", _label.Number);//协议编号

            wiw.WriteIntoDocument("CompanyName", _label.CompanyName);//公司名称
            wiw.WriteIntoDocument("Corporation", _label.Corporation); //法人代表
            wiw.WriteIntoDocument("CompanyAdrr", _label.CompanyAdrr);//住所
            wiw.WriteIntoDocument("RealName", _label.RealName); //投资人（前）
            wiw.WriteIntoDocument("IDCode", _label.IDCode);//投资人身份证（前）

            wiw.WriteIntoDocument("RmBlowercase", _label.RmBlowercase); // 人民币小写
            wiw.WriteIntoDocument("RmBcapital", _label.RmBcapital); //人民币大写
            wiw.WriteIntoDocument("BorrowRate", _label.BorrowRate);//借款年化利率
            wiw.WriteIntoDocument("BorrowLimit", _label.BorrowLimit); //借款期限
            wiw.WriteIntoDocument("ValueDate", _label.ValueDate); //起息日
            wiw.WriteIntoDocument("RefundDay", _label.RefundDay);//还款日
            wiw.WriteIntoDocument("RefundWay", _label.RefundWay);//还款方式(等额本息、按月还款/到期一次性还本付息)
            wiw.WriteIntoDocument("RefundPrincipal", _label.RefundPrincipal);  //月偿还本息金额


            wiw.WriteIntoDocument("CompanyName1", _label.CompanyName1);//公司名称（后）
            wiw.WriteIntoDocument("Corporation1", _label.Corporation1); //法人代表（后）

            wiw.WriteIntoDocument("IDCode1", _label.IDCode1);//投资人身份证（后）
            wiw.WriteIntoDocument("RealName1", _label.RealName1); //投资人（后）
            wiw.WriteIntoDocument("ConcludeDate", _label.ConcludeDate);//协议签订日期

            wiw.WriteIntoDocument("RefundType", _label.RefundType);//变量

            wiw.WriteIntoDocument("HousePlace", _label.HousePlace);//房屋担保位置


            wiw.Save_CloseDocument(SaveDocPath);

            //转成pdf 
            string SavePdfPath = System.AppDomain.CurrentDomain.BaseDirectory + "contract/" + filename + ".pdf";
            var flag = wiw.WordToPdf(SaveDocPath, SavePdfPath);
            if (flag)
            {
                //删除临时word, 
                System.IO.File.Delete(SaveDocPath);
            }
        }















        /*public static MemoryStream FileToMemory(string path)
          {
              try
              {
                  MemoryStream ms = null;
                  using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                  {
                      byte[] infbytes = new byte[(int)fs.Length];
                      fs.Read(infbytes, 0, infbytes.Length);
                      fs.Close();
                      ms = new MemoryStream(infbytes);
                      ms.Position = 0;
                  }
                  return ms;
              }
              catch (Exception)
              {
                  return null;
              }
          }*/

    }
}
